﻿using Score;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreOutput : MonoBehaviour
{
    // 开始计算得分时生成的子对象
    public GameObject Caculator;

    // 舞蹈开始时会获取其生成的子对象Caculator中的scoreCal
    private ScoreCal scoreCal;
    
    //动作开始时间 
    public DateTime starttime;

    public DancingNPC npc;

    bool if_predload = false;


    // Start is called before the first frame update
    void Start()
    {
        //设成min 表示此时评分系统尚未开始计算得分
        starttime = DateTime.MinValue;
        
    }

    /// <summary>
    /// 开始计算得分
    /// 调用此方法,将创建一个ScoreCal游戏对象，令其开始监听当前npc的动作进度
    /// 理论上，npc开始以后的任何时候都可以调用此接口
    /// 建议先使用pre_load以加载npc;pre_load结束后再调用此接口,实现npc的秒启动
    /// </summary>
    /// <param name="time">npc开始舞蹈的准确时间</param>
    /// <param name="npc_path">npc的资源路径</param>
    /// <param name="rate">npc的帧率 默认为60</param>
    public void start_calculating(DateTime time,string npc_path, int rate = 60)
    {
        if (!started())
        {
            if(!if_predload)
                npc = DancingNPC.Get_NPC(npc_path, rate);
            npc.start_dancing(time);
            var caculator = Instantiate(Caculator);
            caculator.transform.parent = gameObject.transform;
            caculator.SetActive(true);
            // 获得其子对象中的ScoreCal组件的引用
            scoreCal = caculator.GetComponent<ScoreCal>();
            starttime = time;
            TimeSpan use_time = DateTime.Now - starttime;
            Debug.Log("激活用时:" + (use_time).ToString());
        }
        else{
            Debug.LogWarning("得分计算器已经激活");
        }
    }

    /// <summary>
    /// 预加载npc模型 在start_calculating前调用 能够极大提高npc启动速度
    /// 首次预加载一般较慢,结束后会自动将预加载模型存在资源目录下,加快之后预加载的速度
    /// </summary>
    /// <param name="npc_path"></param>
    /// <param name="rate"></param>
    public void preLoading_Caculator(string npc_path,int rate = 60)
    {
        DateTime before = DateTime.Now;
        npc = DancingNPC.Get_NPC(npc_path, rate);
        Debug.Log("预加载用时:" + (DateTime.Now - before).ToString());
        if_predload = true;

    }

    // 仅作测试用
    public void load_caculator(string npc_path,DateTime time)
    {
        DateTime before = DateTime.Now;
        npc = DancingNPC.Get_NPC(npc_path);
        npc.start_dancing(time);
        Debug.Log("加载用时:" + (DateTime.Now - before).ToString());
    }



    /// <summary>
    /// 获取当前得分
    /// </summary>
    /// <returns></returns>
    public float getPoint()
    {
        if (started()&&scoreCal!=null)
        {
            if (scoreCal.scores.Count > 0)
            {
                return scoreCal.scores[scoreCal.scores.Count - 1];
            }
            else
            {
                return 0;
            }
        }
        else
        {
            if (!started())
            {
                Debug.LogWarning("尚未激活得分计算器");
            }
            return 0;
        }
        
    }

    public  List<float> getPoints()
    {
        if (started() & scoreCal.scores.Count > 0)
        {
            return scoreCal.scores;
        }
        else
        {
            if (!started())
            {
                Debug.LogWarning("尚未激活得分计算器");
            }
            return null;
        }
    }

    /// <summary>
    /// 得分计算器是否已激活
    /// </summary>
    /// <returns></returns>
    public bool started()
    {
        return starttime != DateTime.MinValue;
    }

    //public bool isPreLoad(string Path)
    //{
    //    return 
    //}

    public bool GetPoint(out float point)
    {

        point = -1.0f;
        return false;
    }
}
